/* ------------------------------------------ routers入口 ------------------------------------------ */

import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

// 自动引入所有的模块
const modules = require.context("./modules", false, /.js$/);
const routes = modules.keys().reduce((result, fileName) => {
  result = [...result, ...modules(fileName).default];
  return result;
}, []);
console.log('路由对象数组', routes)
// 创建router对象
const router = new Router({
  routes: routes
});

// 路由切换处理函数
router.beforeEach((to, from, next) => {
  // 设置标题
  document.title = to.meta.title;
  if(to.name){
    next();
  }else{
    next('/login')
  }
});

export default router;
